package com.hw.dagl.service.impl;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hw.dagl.model.DriverView;
import com.hw.dagl.model.Sys_User;
import com.hw.dagl.model.Vehicle;
import com.hw.dagl.service.ImageItemService;
import com.hw.dao.utils.OraclePageDaoBase;
import com.hw.interceptor.LogAspectBean;
import com.hw.interceptor.SystemLog;

@Service
public class ImageItemServiceImpl extends LogAspectBean implements ImageItemService {

	  @Resource
	  private OraclePageDaoBase oraclePageDaoBase;

	
	@Override
	@SystemLog(Modelname="机动车档案录入",operationType="1",operationName="档案首次录入",detailFlag=true)
	@Transactional(readOnly=false, rollbackFor={Exception.class})
	public boolean insertVehicleRecordAndPic(Vehicle vehicle,Sys_User user) {
		//设置所属地市
		if(user.getGlbm().startsWith("3401")){
			vehicle.setSsds("340100000000");
		}else if(user.getGlbm().startsWith("3402")){
			vehicle.setSsds("340200000000");
		}else if(user.getGlbm().startsWith("3403")){
			vehicle.setSsds("340300000000");
		}else if(user.getGlbm().startsWith("3404")){
			vehicle.setSsds("340400000000");
		}else if(user.getGlbm().startsWith("3405")){
			vehicle.setSsds("340500000000");
		}else if(user.getGlbm().startsWith("3406")){
			vehicle.setSsds("340600000000");
		}else if(user.getGlbm().startsWith("3407")){
			vehicle.setSsds("340700000000");
		}else if(user.getGlbm().startsWith("3408")){
			vehicle.setSsds("340800000000");
		}else if(user.getGlbm().startsWith("3409")){
			vehicle.setSsds("340900000000");
		}else if(user.getGlbm().startsWith("3410")){
			vehicle.setSsds("341000000000");
		}else if(user.getGlbm().startsWith("3411")){
			vehicle.setSsds("341100000000");
		}else if(user.getGlbm().startsWith("3412")){
			vehicle.setSsds("341200000000");
		}else if(user.getGlbm().startsWith("3413")){
			vehicle.setSsds("341300000000");
		}else if(user.getGlbm().startsWith("3415")){
			vehicle.setSsds("341500000000");
		}else if(user.getGlbm().startsWith("3416")){
			vehicle.setSsds("341600000000");
		}else if(user.getGlbm().startsWith("3417")){
			vehicle.setSsds("341700000000");
		}else if(user.getGlbm().startsWith("3418")){
			vehicle.setSsds("341800000000");
		}
		String sql1= "insert into VEHICLE_RECORD_test values (?,?,?,?,?,?,?,?,?,?,?,?,sysdate,sysdate,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),'0',?,'0',?)";
		String sql2= "insert into VEHICLE_PIC_test values (?,?,?)";
		int num1 =oraclePageDaoBase.update(sql1, new Object[]{vehicle.getDabh(),vehicle.getLsh(),vehicle.getXh(),vehicle.getYwlx(),vehicle.getYwyy(),
				vehicle.getHphm(),vehicle.getHpzl(),vehicle.getClpp1(),vehicle.getClsbdh(),vehicle.getSyr(),vehicle.getGlbm(),vehicle.getXzqh(),
				vehicle.getCjr(),vehicle.getBjrq(),vehicle.getSsds(),vehicle.getSerialNumber()});
		
	 int num2=oraclePageDaoBase.update(sql2, new Object[]{vehicle.getXh(),vehicle.getLsh(),vehicle.getTpurl()});
	 return (num1==num2 && num1>=1) ? true : false;
	}
	
	@Override
	@SystemLog(Modelname="机动车档案补录",operationType="2",operationName="档案补录",detailFlag=true)
	@Transactional(readOnly=false, rollbackFor={Exception.class})
	public boolean updateVehicleRecordAndPic(Vehicle vehicle) {
		String sql1= "update VEHICLE_RECORD set gxsj=sysdate,sczt='0' where lsh=? and xh=?";
		String sql2= "update VEHICLE_PIC set tpurl=? where lsh=? and xh=?";
		int num1 =oraclePageDaoBase.update(sql1, new Object[]{vehicle.getLsh(),vehicle.getXh()});
		
	 int num2=oraclePageDaoBase.update(sql2, new Object[]{vehicle.getTpurl(),vehicle.getLsh(),vehicle.getXh()});
	 return (num1==num2 && num1>=1) ? true : false;
	}

	@Override
	public boolean queryVehicleRecordAndPic(Vehicle vehicle) {
		String sql = "select count(*) from VEHICLE_RECORD where lsh='"+vehicle.getLsh()+"'";
		int num = oraclePageDaoBase.queryForInt(sql);
		return num>=1 ? true : false;
	}

	@Override
	@Transactional(readOnly=false, rollbackFor={Exception.class})
	public boolean queryDriverRecordAndPic(DriverView driver) {
		String sql="select count(*) from DRIVER_RECORD where xh='"+driver.getXh()+"'";
		int num = oraclePageDaoBase.queryForInt(sql);
		return num>=1 ? true : false;
	}

	@Override
	@Transactional(readOnly=false, rollbackFor={Exception.class})
	public boolean updateDriverRecordAndPic(DriverView driver) {
		String sql1= "update DRIVER_RECORD set gxsj=sysdate,dazt='1' where xh=?";
		String sql2= "update DRIVER_PIC set tpurl=? where xh=?";
		int num1 =oraclePageDaoBase.update(sql1, new Object[]{driver.getXh()});
		
	 int num2=oraclePageDaoBase.update(sql2, new Object[]{driver.getTpurl(),driver.getXh()});
	 return (num1==num2 && num1>=1) ? true : false;
	}

	@Override
	@Transactional(readOnly=false, rollbackFor={Exception.class})
	public boolean insertDriverRecordAndPic(DriverView driver) {
		String sql1= "insert into DRIVER_RECORD values (?,?,'',?,?,?,?,'',sysdate,sysdate,?,'1')";
		String sql2= "insert into DRIVER_PIC values (?,?)";
		int num1 =oraclePageDaoBase.update(sql1, new Object[]{driver.getXh(),driver.getDabh(),driver.getSfzmhm(),driver.getXm(),
															  driver.getXb(),driver.getFzjg(),driver.getCjr()});
		
	 int num2=oraclePageDaoBase.update(sql2, new Object[]{driver.getXh(),driver.getTpurl()});
	 return (num1==num2 && num1>=1) ? true : false;
	}
	
	
}
